package com.jty.module_user.ui.fragment

import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.Spanned
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.databinding.Observable
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.base.BaseFragment
import com.jty.lib_base.widget.EditTextMonitor
import com.jty.module_user.BR
import com.jty.module_user.R
import com.jty.module_user.databinding.UserFragmentModifyPhoneBinding
import com.jty.module_user.viewmodel.ModifyPhoneViewModel
import com.orhanobut.logger.Logger
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.PublishSubject


@Route(path = AppConstants.Router.User.F_MODIFYPHONE)
class UserModifyPhoneFragment : BaseFragment<UserFragmentModifyPhoneBinding, ModifyPhoneViewModel>() {

    override fun initContentView(): Int {
        return R.layout.user_fragment_modify_phone
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return true
    }

    override fun initViewObservable() {
       viewModel.eyeState.addOnPropertyChangedCallback(object: Observable.OnPropertyChangedCallback()  {
           override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
               Logger.e("监听到了眼睛改变：${propertyId}, eyeState: ${viewModel.eyeState.get()}")
               binding.ivEye.setImageResource(if(viewModel.eyeState.get())R.drawable.ic_eye else R.drawable.ic_eye_hide)
               if (viewModel.eyeState.get()){
                   binding.etLoginPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
//                   binding.etLoginPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
               }else{
                   binding.etLoginPassword.transformationMethod = PasswordTransformationMethod.getInstance()
//                   binding.etLoginPassword.inputType =  InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
               }
           }
       })
        viewModel.uc.ableClickVeri.observe(this){
            binding.tvSendVeri.isClickable = it
        }

    }

    override fun initData() {
        super.initData()
        val photoSubject = PublishSubject.create<String>()
        val pwdSubject = PublishSubject.create<String>()
        val codeSubject = PublishSubject.create<String>()
        binding.etLoginPassword.addTextChangedListener(EditTextMonitor(photoSubject))
        binding.etPhone.addTextChangedListener(EditTextMonitor(pwdSubject))
        binding.etVerification.addTextChangedListener(EditTextMonitor(codeSubject))

        viewModel.addSubscribe(io.reactivex.Observable.combineLatest(
            photoSubject,
            pwdSubject,
            codeSubject
        ) { account: String, pwd: String, code:String->
            account.isNotBlank() && pwd.isNotBlank() && code.isNotBlank()
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                binding.btnModify.isEnabled = it
                binding.btnModify.setBackgroundResource(if (it) R.drawable.shape_round_white else R.drawable.green_btn_corner_10dp)
            }
        )

        val filter = object : InputFilter {
            override fun filter(
                p0: CharSequence?,
                p1: Int,
                p2: Int,
                p3: Spanned?,
                p4: Int,
                p5: Int
            ): CharSequence {
                return p0?.let{if(it.equals(" ")) "" else it}?:""

            }

        }
        binding.etLoginPassword.filters = arrayOf(filter, LengthFilter(12))


    }
}